<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

if (!$teammap instanceof LM_Table_Teammap) {
    LM::redirect();
}

$eventtypemap = LM_Eventtypemap::getInstance();

switch ($task) {

    case 'change':
        LM::setTeam(intval(mosGetParam($_POST, "_team_", 0)));
        LM::redirect("playermap");
        break;

    case 'edit':

        $id = intval(mosGetParam($_REQUEST, 'id', $cid[0]));

        $row = LM::getLMTable("Playermap2");
        $row->load($id);

        $player = LM::getLMTable("Player");
        $player->load($row->playermap_player);

        $positions[] = mosHTML::makeOption('0', 'Select Position');

        $database->setQuery(
                "SELECT position_id AS value, position_name AS text
                   FROM #__leagueman_position
               ORDER BY ordering");
        $positions = array_merge($positions, $database->loadObjectList());

        $template = LM::getTemplate();
        $template->assign("view", "admin/form_playermap.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("my", $my);
        $template->assign("row", $row);
        $template->assign("player", $player);
        $template->assign("positions", $positions);

        $template->display("admin/view.tpl.php");
        break;

    case 'unassign':

        $row = LM::getLMTable("Playermap");

        $total = count($cid);

        $i = 0;
        foreach ($cid AS $id) {
            if ($row->delete($id)) {
                $i++;
            }
        }

        LM::redirect("playermap", "$i players unassigned");

        break;

    case 'reset':

        $total = count($cid);
        for ($i = 0; $i < $total; $i++) {
            $query = "UPDATE #__leagueman_playermap
                         SET playermap_position = 0,
                             position_number = 0,
                             cnt_matches = 0,
                             cnt_in = 0,
                             cnt_out = 0,
                             cnt_goals = 0,
                             cnt_own_goals = 0,
                             cnt_assists = 0,
                             cnt_yellow = 0,
                             cnt_yellow_red = 0,
                             cnt_red = 0
                       WHERE playermap_id = $cid[$i] LIMIT 1";
            $database->setQuery($query);

            if (!$database->query()) {
                LM::redirect("playermap", $database->getErrorMsg());
                exit();
            }
        }

        $msg = 'Data resetted';
        LM::redirect("playermap", $msg);
        break;

    case 'savelist':

        $total = count($cid);

        for ($i = 0; $i < $total; $i++) {

            $playermap_position = $database->Quote(intval($_POST["playermap_position"][$i]));
            $position_number = $database->Quote(intval($_POST["position_number"][$i]));
            $cnt_matches = $database->Quote(intval($_POST["cnt_matches"][$i]));

            $settings = array(
                "playermap_position = $playermap_position",
                "position_number    = $position_number",
                "cnt_matches        = $cnt_matches"
            );

            foreach ($project->getEventtypes() as $e) {

                $eventtype = $eventtypemap->getEventtype($e);

                $key = $eventtype->playermap_association;
                $value = $database->Quote(intval($_POST[$key][$i]));

                $settings[] = $key . " = " . $value;
            }

            $query = "UPDATE #__leagueman_playermap
                         SET " . implode(", ", $settings) . "
                       WHERE playermap_id		= $cid[$i]";
            $database->setQuery($query);

            if (!$database->query()) {
                LM::redirect("playermap", $database->getErrorMsg());
            }
        }

        LM::redirect("playermap");
        break;

    case 'save':
        $row = LM::getLMTable("Playermap");
        $row->save($_POST);

        $image = saveImage($mainframe->getCfg("absolute_path") . "/images/stories/leagueman/players/", "upload", "pm{$row->getID()}");

        if ($image) {
            $row->playermap_image = $image;
        }

        if ($row->store()) {
            $msg = "OK";
        } else {
            $msg = "Could not save Player";
        }

        LM::redirect("playermap", $msg);
        break;

    case 'cancel':
        LM::redirect("playermap");
        break;

    default:

        $limit      = LM::getSessionValueFromRequest("_{$act}_limit", 'limit', 50);
        $limitstart = LM::getSessionValueFromRequest("_{$act}_limitstart", 'limitstart', 0);

        $playertable = LM::getLMTable("Playermap");
        $playertable->filterbyProject($project->getID());
        $playertable->filterbyTeam($team->getID());

        $pageNav = LM::getPageNav($playertable->getTotalRows(), $limitstart, $limit, 1);

        $rows = $playertable->getRows("", $limitstart, $limit);

        $positions[] = mosHTML::makeOption('0', 'Select Position');
        $database->setQuery("SELECT position_id AS value, position_name AS text FROM #__leagueman_position ORDER BY ordering");
        $positions = array_merge($positions, $database->loadObjectList());

        $team_list = $project->getTeamToolBox();

        $template = LM::getTemplate();
        $template->assign("view", "admin/list_playermap.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("teammap", $teammap);
        $template->assign("eventtypemap", $eventtypemap);
        $template->assign("team_list", $team_list);
        $template->assign("pageNav", $pageNav);
        $template->assign("my", $my);
        $template->assign("rows", $rows);
        $template->assign("positions", $positions);

        $template->display("admin/view.tpl.php");

        break;
}